//
//  TipsPOPView.swift
//  TuneMate
//
//  Created by 丸子 on 2023/11/24.
//

import UIKit
enum TipsStyle:Int {
    case oneBtn = 1
    case twoBtn = 2
    case delete = 3
}

class TipsPOPView: UIView {
    let disposeBag = DisposeBag()
    var chooseDone: (() -> Void)?
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.text = ""
        label.font = UIFont.pingFangSCSemibold(ofSize: 18)
        label.textColor = .white
        label.textAlignment = .center
        return label
    }()
    
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.textAlignment = .center
        label.font = UIFont.pingFangSCRegular(ofSize: 16)
        label.textColor = Theme.color.text_d4
        return label
    }()
    
    private lazy var cancelButton: UIButton = {
        let button = UIButton()
        button.setTitle("取消", for: .normal)
        button.setTitleColor(UIColor.white, for: .normal)
        button.titleLabel?.font = UIFont.pingFangSCRegular(ofSize: 16)
        button.backgroundColor = UIColor.black.withAlphaComponent(0.25)
        button.allRound(25)
        return button
    }()
    
    private lazy var confirmButton: UIButton = {
        let button = UIButton()
        button.setTitle("确定", for: .normal)
        button.setTitleColor(Theme.color.text_d1, for: .normal)
        button.titleLabel?.font = UIFont.pingFangSCRegular(ofSize: 16)
        button.allRound(25)
        return button
    }()
    
    private lazy var alertView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hexString: "#221D30")
        view.layer.cornerRadius = 10
        view.layer.masksToBounds = true
        return view
    }()
    
    init() {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
        self.backgroundColor = UIColor(hexString: "#000000", transparency: 0.3)
        addSubview(alertView)
        bindEvent()
    }
    
    func bindEvent(){
        cancelButton.rx.tap.subscribe(onNext: { [weak self] _ in
            guard let self = self else { return }
            UIView.animate(withDuration: 0.3, animations: {
                self.alertView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
                self.alpha = 0
            },completion: { _ in
                self.removeFromSuperview()
            })
        }).disposed(by: disposeBag)
        
        confirmButton.rx.tap.subscribe(onNext: { [weak self] _ in
            guard let self = self else { return }
            UIView.animate(withDuration: 0.3, animations: {
                self.alertView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
                self.alpha = 0
            },completion: { _ in
                self.removeFromSuperview()
                self.chooseDone?()
            })
        }).disposed(by: disposeBag)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initViews(title:String, desc:String, style:TipsStyle) {
        alertView.addSubviews([titleLabel, contentLabel, cancelButton, confirmButton])
        
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(24)
        }
        
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(16)
            make.leading.equalToSuperview().offset(20)
            make.trailing.equalToSuperview().offset(-20)
        }
         
        if style == .oneBtn {
            confirmButton.snp.makeConstraints { (make) in
                make.leading.trailing.equalToSuperview().inset(90)
                make.top.equalTo(contentLabel.snp.bottom).offset(32)
                make.height.equalTo(44)
            }
            cancelButton.isHidden = true
        }else {
            
            cancelButton.snp.makeConstraints { (make) in
                make.leading.equalToSuperview().offset(16)
                make.top.equalTo(contentLabel.snp.bottom).offset(32)
                make.height.equalTo(50)
            }
            
            confirmButton.snp.makeConstraints { (make) in
                make.trailing.equalToSuperview().offset(-16)
                make.width.equalTo(cancelButton.snp.width)
                make.leading.equalTo(cancelButton.snp.trailing).offset(12)
                make.top.equalTo(contentLabel.snp.bottom).offset(32)
                make.height.equalTo(50)
            }
        }
        
        alertView.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(24)
            make.centerY.equalToSuperview()
            make.top.greaterThanOrEqualTo(titleLabel.snp.top).offset(-24)
            make.bottom.greaterThanOrEqualTo(confirmButton.snp.bottom).offset(24)
        }
        
        alertView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
        UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
            self.alertView.transform = .identity
        }, completion: nil)
        
        
        if style == .delete {
            confirmButton.backgroundColor = UIColor.black.withAlphaComponent(0.25)
            confirmButton.setTitleColor(UIColor(hexString: "#FF4242"), for: .normal)
        }else {
            confirmButton.themeGradient(nil, .leftToRight)
        }
        
        titleLabel.text = title
        contentLabel.text = desc
        
    }
}
